package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddBookMapper {
    /**
     * 获取用户的地址簿
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId}")
    List<AddressBook> getAll(Long userId);

    /**
     * 新增地址簿
     * @param addressBook
     */
    @Insert("insert into address_book (id,user_id,consignee,sex,phone,province_code,province_name," +
            "city_code,city_name,district_code,district_name,detail,label,is_default) " +
            "values(#{id},#{userId},#{consignee},#{sex},#{phone},#{provinceCode},#{provinceName}," +
            "#{cityCode},#{cityName},#{districtCode},#{districtName},#{detail},#{label},#{isDefault})")
    void insert(AddressBook addressBook);

    /**
     * 根据Id查询地址簿
     * @param id
     * @return
     */
    @Select("select * from address_book where id = #{id};")
    AddressBook getById(Long id);

    /**
     * 更新地址簿
     * @param addressBook
     */
    void update(AddressBook addressBook);

    /**
     * 根据Id删除地址簿
     * @param id
     */
    @Delete("delete from address_book where id = #{id}")
    void deleteById(Long id);

    /**
     * 查询默认地址簿
     * @param addressBook
     * @return
     */
    List<AddressBook> getDefault(AddressBook addressBook);

    @Update("update address_book set is_default = 0")
    void setAllDefault();
}
